var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var Auth = require('../../utils/auth.js');

var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
var app = getApp();
var webSiteName= config.getWebsiteName;
var domain =config.getDomain
import config from '../../utils/config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight:'100%',
    date:'请选择开始时间',
    date2:'请选择结束时间',
    list:[],
    keyword:'',
    primary_mobile:'',
    page :1,
    page_total:0,
    type:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //需要用户登录
    var self = this;
    self.setData({
      type:options.type,
      keyword:options.keyword,
      primary_mobile:mobile
    })

    wx.getSystemInfo({
      success: (result) => {
        self.setData({
          windowHeight:result.windowHeight,
          windowWidth:result.windowWidth
        })
        console.log(result.inFinChat);
        if(result.inFinChat)
        {          
          self.setData({inFinChat:result.inFinChat})          
        }
        else{          
          Auth.setUserInfoData(self);
          Auth.checkLogin(self);
          self.setData({copyright: getApp().globalData.copyright})
          Auth.checkSession(self, 'isLoginNow');
        }
      },
    })
    let mobile = self.data.userInfo.user_mobile;
    if(!mobile){
        mobile = self.data.primary_mobile
    }
    wx.request({
      url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/list',
      method:'post',
      data:{
        key_words:options.keyword,
        type:self.data.type,
        mobile:mobile,
        token:self.data.userInfo.token,
        page:self.data.page
      },
      success:function(res){
        if(res.data.status == 200){
          self.setData({
            page_total:res.data.total,
            list:res.data.rows
          })
        }else{

        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindDateChange: function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindDateChange2: function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date2: e.detail.value
    })
  },
  goToDetail:function(e){
    wx.navigateTo({
      url: '/pages/warrantyDetail/detail?id='+e.target.dataset.id,
    })
  },
  bindscrolltolower:function(e){ 
    var that = this;
    console.log(that.data.page_total);
    console.log(that.data.page);
    if(that.data.page_total>=that.data.page){
      var now_page = that.data.page+1;
      that.setData({
        page:now_page
      })
    
      that.appendList();
    }

  },
  
  getinputVlue:function(e){
    this.setData({
      keyword:e.detail.value
    })
  },
  searchList:function(e){
    var self = this;

    let mobile = self.data.userInfo.user_mobile;
    if(!mobile){
        mobile = self.data.primary_mobile
    }
    
    wx.request({
      url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/list',
      method:'post',
      data:{
        search:self.data.keyword,
        type:self.data.type,
        date_start:self.data.date,
        date_end:self.data.date2,
        mobile:mobile,
        token:self.data.userInfo.token,
        page:1
      },
      success:function(res){
          var list = [];
          for(let i in res.data.rows){
            list.push( res.data.rows[i])
          }
          self.setData({
            list:list,
            page_total: res.data.total
          })
          
      }
    })
  },
  appendList:function(e){
    var self = this;

    let mobile = self.data.userInfo.user_mobile;
    if(!mobile){
        mobile = self.data.primary_mobile
    }
    
    wx.request({
      url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/list',
      method:'post',
      data:{
        search:self.data.keyword,
        type:self.data.type,
        date_start:self.data.date,
        date_end:self.data.date2,
        mobile:mobile,
        token:self.data.userInfo.token,
        page:self.data.page
      },
      success:function(res){
        var list = self.data.list;

        if(res.data.rows <= 0){

            wx.showToast({
                title: '没有更多数据了!',
                icon: 'none',
                duration: 2000
            });

            return false;
        }

        for(let i in res.data.rows){
            list.push( res.data.rows[i])
        }
        self.setData({
            list:list
        })
      }
    })
  }
})